' *** Documentation
' 
' Klenwell VB Script Template
' 
' last updated: Jun 2007
' author: klenwell@gmail.com
' 
' DESCRIPTION
'  test objects in vbs
' 
' NOTES
' 	Test PrintR by creating an array of 2 Dictionary, another array, and 3 vars
' ____________________________________________________________________________*/

' *** DECLARATIONS
Option Explicit

' Show Errors?
Const SHOW_ERRORS = True
Const HTML_BR = "<br />"

' *** SCRIPT ERROR HANDLER
' Config
dim oDictErr, iErrCount
Set oDictErr = CreateObject("Scripting.Dictionary")
iErrCount = 0

' Klenwell Error Trigger
Function kwTriggerError(sMessage)
	iErrCount = iErrCount + 1
	oDictErr.Add iErrCount, sMessage
	If SHOW_ERRORS Then: MsgBox("Error #" & iErrCount & " : " & sMessage)
	Err.Clear()
End Function

' Klenwell Error Dump
Sub kwDumpErrors
	dim sOut, vKey
	sOut = "Error Dump ("&iErrCount&" Errors): " & VbCrLf
	For Each vKey In oDictErr
		sOut = sOut & "Error " & vKey & " in oDictErr: " & oDictErr.Item(vKey) & VbCrLf
	Next
	MsgBox(sOut)
End Sub
' *** END SCRIPT ERROR HANDLER


' *** FILE INCLUDE HANDLER
' Set Files to Include Here
dim aINCLUDE(1)
aINCLUDE(1) = "printr.inc.vbs"

' include file
Function kwIncludeFile(sFilePath)
'On Error Resume Next
Dim oFsoInc, oFileHandler, sIncContent
Set oFsoInc = CreateObject ("Scripting.FileSystemObject")

If sFilePath = "" Then: Exit Function: End If

If NOT oFsoInc.FileExists(sFilePath) Then
kwTriggerError("unable to load " & sFilePath & " : file not found")
Exit Function
End If

Set oFileHandler = oFsoInc.OpenTextFile(sFilePath,1)
If Err.number <> 0 Then
kwTriggerError("file " & sFilePath & " could not be opened")
Exit Function
End If

sIncContent = oFileHandler.ReadAll
oFileHandler.close
ExecuteGlobal sIncContent

Set oFsoInc = Nothing
Set oFileHandler = Nothing

End Function

' include all files
Sub kwIncludeFiles
dim sFile
For Each sFile In aINCLUDE
'MsgBox("attempting to load: " & sFile)
kwIncludeFile(sFile)
Next
End Sub

Call kwIncludeFiles
'Call kwDumpErrors


' *** SCRIPT
' Create 2 Dictionary Objects
Dim oDict1, oDict2

	Set oDict1 = CreateObject("Scripting.Dictionary")
	Set oDict2 = CreateObject("Scripting.Dictionary")

	' Add a random number of random items
	Dim i, iNumItems, iRand

	' Dict 1
	iNumItems = 4
	For i = 1 to iNumItems
		Randomize: iRand = Int((1000 - 1 + 1) * Rnd + 1)
		oDict1.Add i, iRand
	Next
	
	' Dict 2
	iNumItems = 8
	For i = 1 to iNumItems
		Randomize: iRand = Int((1000 - 1 + 1) * Rnd + 1)
		oDict2.Add i, iRand
	Next

	
' Create Sub-Array
Dim ArrSubArray(5)
	ArrSubArray(1) = "whatever"
	ArrSubArray(2) = 5
	ArrSubArray(3) = 1.5
	ArrSubArray(4) = "another string followed by a boolean"
	ArrSubArray(5) = False


' Create Test Array
Dim ArrTest(7)

	ArrTest(1) = "a string"
	ArrTest(2) = ArrSubArray
	Set ArrTest(3) = oDict1
	ArrTest(4) = True
	Set ArrTest(5) = oDict2
	ArrTest(6) = 42
	Set ArrTest(7) = CreateObject("Scripting.FileSystemObject")


Dim sArrayDump, sArrayDump2, sArrayDump3, sArrayDump4
MsgBox("Begin the PrintR Test")
sArrayDump = vbPrintR(ArrTest, "ArrTest")
sArrayDump2 = vbPrintR(ArrSubArray, "ArrSubArray")
sArrayDump3 = vbPrintR(oDict1, "oDict1")
sArrayDump4 = vbPrintR(ArrSubArray(4), "ArrSubArray Element")
MsgBox(sArrayDump)
MsgBox(sArrayDump2)
MsgBox(sArrayDump3)
MsgBox(sArrayDump4)
